package com.ql.mangersystem.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ql.mangersystem.common.HoneyLogs;
import com.ql.mangersystem.common.LogType;
import com.ql.mangersystem.common.Page;
import com.ql.mangersystem.common.Result;
import com.ql.mangersystem.entity.dto.OrdersPageDTO;
import com.ql.mangersystem.entity.dto.OrdersUpdateDTO;
import com.ql.mangersystem.entity.po.Orders;
import com.ql.mangersystem.entity.po.User;
import com.ql.mangersystem.service.OrdersService;
import com.ql.mangersystem.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/orders")
@RestController
@Slf4j
@Api("账单接口")
@CrossOrigin(value = "http://localhost:5173/")
public class OrdersController {
    @Resource
    private OrdersService ordersService;

    @ApiOperation("新增订单")
    @PostMapping("/add")
    @HoneyLogs(operation = "订单", type = LogType.ADD)
    public Result add(@RequestBody Orders orders) {
        User currentUser = TokenUtils.getCurrentUser();  // 获取到当前登录的用户信息
        assert currentUser != null;
        orders.setUserid(currentUser.getId());
        orders.setDate(DateUtil.today());  //   2023-10-08
        orders.setNo(IdUtil.fastSimpleUUID());
        int result = ordersService.save(orders);
        if (result<=0){
            return Result.error("添加失败");
        }
        return Result.success();
    }
    @ApiOperation("根据id查询")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id,@RequestParam("userid")Integer userid) {
        Orders orders = ordersService.getById(id,userid);
        if (orders==null){
            return Result.error("查询失败");
        }
        return Result.success(orders);
    }
    @ApiOperation("修改订单")
    @HoneyLogs(operation = "订单", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody Orders orders) {
        orders.setDate(DateUtil.today());
      int result=ordersService.update(orders);
      if (result<=0){
          return Result.error("修改失败");
      }
      return Result.success();
     }
     @ApiOperation("删除订单")
     @HoneyLogs(operation = "订单", type = LogType.DELETE)
     @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        int result = ordersService.delete(id);
        if (result<=0){
            return Result.error("删除失败");
        }
        return Result.success();
     }
    @ApiOperation("批量删除信息")
    @HoneyLogs(operation = "订单", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestParam("ids") List<Integer> ids,@RequestParam("userid")Integer userid) {
        int result = ordersService.deleteBatch(ids,userid);
        if (result<=0){
            return Result.error("删除失败");
        }
        return Result.success();
    }
    @ApiOperation("模糊分页查询")
    @PostMapping("/page")
    public Result page(@RequestBody OrdersPageDTO ordersPageDTO) {
        Page<Orders> page = ordersService.page(ordersPageDTO);
        if (page==null){
            return Result.error("查询失败");
        }
        return Result.success(page);
    }
}
